module( ..., package.seeall )

--private for this module
local function db_fmt(value)
	saved = {}
	if type(value) == type({}) then
		local retval = ''
		retval = retval .. '{'
		local visited = {}
		for i, v in ipairs(value) do
			if saved[v] and type(v) == type({}) then
				retval = retval .. '<visited>' .. ', '
			else
				saved[v] = true
				retval = retval .. db_fmt( v, saved ) .. ', '
			end
			visited[i] = true
		end
		for k, v in pairs(value) do
			if visited[k] == nil then
				if saved[v] and type(v) == type({}) then
					retval = retval .. '[' .. db_fmt(k, saved) .. '] = ' .. '<visited>' .. ', '
				else
					saved[v] = true
					retval = retval .. '[' .. db_fmt(k,saved) .. '] = ' .. db_fmt( v,saved ) .. ', '
				end
			end
		end
		retval = retval .. '}'
		return retval 
	elseif type(value) == type('') then
		return(string.format("'%s'",value))
	elseif value ~= nil then
		return(string.format("%s",tostring(value)))					
	elseif value == nil then
		return 'nil'
	end
	return "<unknown>"
end

function d(s)
	print(db_fmt(s))	
end

function print_byte(str)
	MOD_DEBUG.d("begin byte", str, string.len(str))
	local ByteCount = string.len(str)
	local ByteIndex = 1
	while (ByteIndex <= ByteCount) do
		MOD_DEBUG.d(string.byte(string.sub(str, ByteIndex, ByteIndex)))
		ByteIndex = ByteIndex + 1
	end
	MOD_DEBUG.d("end byte")
end

function Log(data)
	local file = WRITE_PATH.."log.txt"
	local f = io.open(file,'a+')
	if f then
		local tab=os.date("*t",time);
		local str = tab.hour..":"..tab.min..":"..tab.sec.."   "..tostring(data)
		f:write(str)
		f:write("\r\n")
		f:close()
	end		
end
